package com.xbz.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * author: xinmu
 * date: 2022/5/10
 * time: 13:21
 */

//代码生成
public class Generator {
    public static void main(String[] args) {

        //需要构建一个代码自动生成器
        AutoGenerator generator = new AutoGenerator();

        //配置策略

        //1.配置策略
        GlobalConfig global = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //global.setOutputDir(projectPath+"/admin/src/main/java"); //生成到哪
        //推荐使用绝对路径
        global.setOutputDir("E:\\cloud\\OneDrive - stu.cdut.edu.cn\\桌面\\gitclone-temp\\xbz\\xianbuzhu\\common\\src\\main\\java");
        global.setAuthor("xinmu123");
        global.setOpen(false); //是否打开资源管理器
        global.setFileOverride(false); //是否覆盖
        global.setServiceName("%sService"); //去service的I前缀
        global.setIdType(IdType.ID_WORKER);
        global.setDateType(DateType.ONLY_DATE);
        global.setSwagger2(true);
        generator.setGlobalConfig(global);

        //2.数据源配置(确保正确!!!!)
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/xbz?userSSL=false&userUnicode=true&characterEncoding=utf-8");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        generator.setDataSource(dsc);

        //3.包名的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.xbz");
        packageConfig.setModuleName("common");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        generator.setPackageInfo(packageConfig);

        //策略配置
        StrategyConfig otherConfig = new StrategyConfig();
        //设置要映射的表名(可以同时写多张表名)
        otherConfig.setInclude("sys_user");
        //下划线改驼峰命名
        otherConfig.setNaming(NamingStrategy.underline_to_camel);
        otherConfig.setColumnNaming(NamingStrategy.underline_to_camel);

        //生成实体类时去掉表前缀
        //otherConfig.setTablePrefix(packageConfig.getModuleName()+"_");
        otherConfig.setTablePrefix("sys_");
        //开启Lombok
        otherConfig.setEntityLombokModel(true);
        //逻辑删除
        otherConfig.setLogicDeleteFieldName("deleted");

        //自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        otherConfig.setTableFillList(tableFills);

        //乐观锁
        //otherConfig.setVersionFieldName("version");

        otherConfig.setRestControllerStyle(true);
        otherConfig.setControllerMappingHyphenStyle(true); //下划线命名

        generator.setStrategy(otherConfig);

        generator.execute();//执行


    }
}
